package com.bosssoft.ecds.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.ecds.common.response.CommonCode;
import com.bosssoft.ecds.common.response.QueryResponseResult;
import com.bosssoft.ecds.common.response.ResponseResult;
import com.bosssoft.ecds.config.StatusCode;
import com.bosssoft.ecds.entity.po.StockReturnItemPO;
import com.bosssoft.ecds.entity.po.StockReturnPO;
import com.bosssoft.ecds.entity.vo.DateVO;
import com.bosssoft.ecds.entity.vo.PageVO;
import com.bosssoft.ecds.entity.vo.StockReturnItemVO;
import com.bosssoft.ecds.entity.vo.StockReturnVO;
import com.bosssoft.ecds.service.StockReturnItemService;
import com.bosssoft.ecds.service.StockReturnService;
import com.bosssoft.ecds.service.impl.StockReturnServiceImpl;
import com.bosssoft.ecds.util.PageResult;
import com.bosssoft.ecds.util.ResponseUtils;
import com.bosssoft.ecds.util.SnowFlakeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 * <p>
 * 类功能描述：退票主表
 *
 * @author ZhuWen
 * @since 2020-08-11
 */
@RestController
@RequestMapping("/stock-return-po")
@Slf4j
public class StockReturnController {

    @Autowired
    StockReturnServiceImpl stockReturnService;

    @Autowired
    StockReturnItemService stockReturnItemService;

    /**************************退票主表和退票明细表进行多表联合操作****************************/

    /**
     * 雪花算法獲取NO
     * @return
     */
    @GetMapping("/getNo")
    public QueryResponseResult<Long> getNo() {
        Long no = SnowFlakeUtils.genId();
        System.out.println("業務單號："+no);
        return new QueryResponseResult<>(CommonCode.SUCCESS, no);
    }

    /**
     * 添加退票申请
     *
     * @param stockReturnVO 退票VO
     * @return
     */
    @PostMapping("/addStockReturn")
    public ResponseResult addStockReturn(@RequestBody StockReturnVO stockReturnVO) {
        String status = stockReturnService.addStockReturnPO(stockReturnVO);
        return getResponseResult(status);
    }

    /**
     * 列出所有的退票主表信息
     *
     * @return
     */
    @GetMapping("/getListStockReturn")
    public List<StockReturnVO> getStockReturn() {
        return stockReturnService.stockRetrunVOList();
    }

    /**
     * 分页查询
     * @param dateVO
     * @return
     */
    @PostMapping("/getListStockReturnPage")
    public QueryResponseResult<PageResult> getPageReslt(@RequestBody DateVO dateVO){
        /*return stockReturnService.stockReturnVOListByPage(dateVO);*/
        PageResult pageResult = stockReturnService.stockReturnVOListByPage(dateVO);
        return new QueryResponseResult<>(CommonCode.SUCCESS, pageResult);
    }

    /**
     * 根据业务单号查询退票明细信息
     *
     * @param no 业务单号
     * @return
     */
    @GetMapping("/getListStockReturnItem")
    public List<StockReturnItemVO> getStockReturnItem(Long no) {
        return stockReturnService.stockReturnItemVOList(no);
    }

    /**
     * 根据业务单号查询单条退票主信息以及附属多条明细信息
     * @param stockReturnVO
     * @return
     */
    @PostMapping("/getStockReturnByNo")
    public QueryResponseResult<StockReturnVO> getStockReturn(@RequestBody StockReturnVO stockReturnVO) {
        /*return stockReturnService.stockRetrunVOByNo(stockReturnVO);*/
        StockReturnVO stockReturnVO1 = stockReturnService.stockRetrunVOByNo(stockReturnVO);
        return new QueryResponseResult<>(CommonCode.SUCCESS, stockReturnVO1);
    }

    /**
     * 根据业务单号或者编制日期区间查询
     * @param no
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @GetMapping("/getListStockReturnByDateOrNo")
    public List<StockReturnVO> getStockReturnListByDate(Long no, String startTime, String endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime1 = simpleDateFormat.parse(startTime);
        Date endTime1 = simpleDateFormat.parse(endTime);
        // log.info("时间：" + dateVO.getStartTime().toString());
        return stockReturnService.stockReturnVOlistByDate(no, startTime1, endTime1);
    }

    @PostMapping("/getListStockReturnByDateOrNo1")
    public List<StockReturnVO> getStockReturnListByDate(@RequestBody  DateVO dateVO) {
        return stockReturnService.stockReturnVOlistByDate1(dateVO);
    }

    /**
     * 根据No修改退票主信息
     *
     * @param stockReturnVO 退票信息VO
     * @return
     */
    @PostMapping("/updateByNo")
    public ResponseResult updateByNo(@RequestBody StockReturnVO stockReturnVO) {
        String status = stockReturnService.updateStockReturn(stockReturnVO);
        return getResponseResult(status);
    }

    /**
     * 修改退票明细信息
     *
     * @param stockReturnItemVOList 退票明细VO集合
     * @return
     */
    @PutMapping("/updateItemByNo")
    public ResponseResult updateItemByNo(@RequestBody List<StockReturnItemVO> stockReturnItemVOList, Long no) {
        String status = stockReturnService.updateStockReturnItemPO(stockReturnItemVOList, no);
        return getResponseResult(status);
    }

    /**
     * 删除退票申请
     *
     * @param no 业务单号
     * @return
     */
    @GetMapping("/deleteByNo")
    public ResponseResult deleteByNo(Long no) {
       String status = stockReturnService.deleteStockReturn(no);
       return getResponseResult(status);
    }

    /**
     * 删除退票明细
     *
     * @param no
     * @return
     */
    @DeleteMapping("/deleteItemByNo")
    public String deleteItemByNo(Long no) {
        QueryWrapper<StockReturnItemPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_pid", no);
        boolean status = stockReturnItemService.remove(queryWrapper);
        if (status == true) {
            return StatusCode.DELETE_SUCCESS;
        } else {
            return StatusCode.DELETE_FAILED;
        }
    }

    /**
     * 确定是否提交 0未提交，1已经提交
     *
     * @param no 业务单号
     * @return
     */
    @GetMapping("/getSubmitStatus")
    public Integer getSubmitStatus(Long no) {
        StockReturnPO stockReturnPO = stockReturnService.getOne(new QueryWrapper<StockReturnPO>().eq("f_no", no));
        return stockReturnPO.getSubmitStatus();
    }

    /**
     * 提交
     *利用消息中间件发消息财政端使财政端增添一条新的退票主表信息
     * @param no
     * @return
     */
    @PutMapping("/updateSubmitStatus")
    public ResponseResult updateSubmitStatus(Long no) {
        StockReturnPO stockReturnPO = new StockReturnPO();
        stockReturnPO.setSubmitStatus(1);
        boolean status = stockReturnService.update(stockReturnPO, new QueryWrapper<StockReturnPO>().eq("f_no", no));
        if (status == true) {
            log.info("在提交时，单位端数据更新成功");
            StockReturnVO stockReturnVO = new StockReturnVO();
            stockReturnVO.setNo(no);
            /** 要发给财政端的StcokReturnVO1,包含退票主表信息和退票明细信息*/
            StockReturnVO stockReturnVO1 = stockReturnService.stockRetrunVOByNo(stockReturnVO);
            //通过中间件向财政端发送消息，待实现
            stockReturnService.submit(stockReturnVO1);
            return ResponseResult.SUCCESS();
        }
        return ResponseResult.FAIL();
    }

    /**
     * 查询审核状态 0未审核，1已审核通过，2审核未通过，3无需审核
     *
     * @param no 业务单号
     * @return
     */
    @GetMapping("/getCheckStatus")
    public Integer saveInfo(Long no) {
        StockReturnPO stockReturnPO = stockReturnService.getOne(new QueryWrapper<StockReturnPO>().eq("f_no", no));
        return stockReturnPO.getChangeState();
    }

    /**
     * 根据财政端发送的PO对象修改单位端的PO字段属性，并且更改对应的单位票据主表是否退票字段
     * @param stockReturnPO 财政端发送送PO对象
     * @return
     */
    @PostMapping("/apcetUpdate")
    public ResponseResult apcetUpdate(@RequestBody  StockReturnPO stockReturnPO){
        String status = stockReturnService.apcetUpdate(stockReturnPO);
        return getResponseResult(status);
    }

    /**
     * 返回前端状态码
     * @param status
     * @return
     */
    private ResponseResult getResponseResult(String status) {
        if(status.equals(StatusCode.ADD_SUCCESS) || status.equals(StatusCode.DELETE_SUCCESS) ||
                status.equals(StatusCode.FIND_SUCCESS) || status.equals(StatusCode.UPDATE_SUCCESS)) {
            return ResponseResult.SUCCESS();
        }
        return ResponseResult.FAIL();
    }
}

